//地图相关计算
window._MANAGERMAP = {

    //地图单元格长度
    CELLSIZE:64,

    DIRARR:[
                //[-1,-1],
                [0,-1],
                //[1,-1],
                [-1,0],        
                [1,0],
                //[-1,1],
                [0,1],
                //[1,1]
            ],

    //获取移动路径
    getMovePoint:function(tiledMap,sx,sy,tx,ty,h,w){
        
        if(_MANAGERMAP.isMapPointCanTouchByMap(tiledMap,tx,ty,h,w)){

            return  _MANAGERMAP.loopFindPath(tiledMap,[{x:sx,y:sy}],tx,ty,h,w,{});

        }

        return null;

    },


    loopFindPath:function(tiledMap,points,tx,ty,h,w,checkMap){

        //console.log('搜索节点',points);

        var nextpoints = [];

        for(var i=0;i<points.length;i++){

            var p = points[i];
            var px = p.x;
            var py = p.y;
            
            for(var j=0;j<_MANAGERMAP.DIRARR.length;j++){

                var dir = _MANAGERMAP.DIRARR[j];
                var npx = px+dir[0];
                var npy = py+dir[1];

                //console.log('方位',dir,px,py,npx,npy);

                if(npx==tx&&npy==ty){
                    //var newpoint = {x:npx,y:npy,lastp:p};
                    //return newpoint;
                    var path = [[npx,npy]];
                    var pathp = p;
                    while(pathp!=null){
                        if(pathp.lastp){
                            path.unshift([pathp.x,pathp.y]);
                        }
                        pathp = pathp.lastp;
                    }
                    return path;
                }else{
                    if(
                        !checkMap[npx+"-"+npy]
                        &&npx>=0&&npy>=0&&npy<h&&npx<w
                        &&_MANAGERMAP.isMapPointCanTouchByMap(tiledMap,npx,npy,h,w)
                        &&!_MANAGERMAP.isMapPointHashObj(npx,npy)
                    ){
                        checkMap[npx+"-"+npy]=1;
                        nextpoints.push({x:npx,y:npy,lastp:p});
                    }
                }

            }


        }

        if(nextpoints.length>0){
            return _MANAGERMAP.loopFindPath(tiledMap,nextpoints,tx,ty,h,w,checkMap);
        }else{
            return null;
        }


    },

    //根据地图相对坐标判断单元格是否能移动
    isMapPointCanTouchByMap:function(tiledMap,x,y,h,w){

        if(x>=w||y>=h||x<0||y<0){
            return false;
        }

        var titlepos = cc.v2(x,y);

        for(var key in _CS.wallSkill){

            if(tiledMap.getLayer(key)){

                var sign = _CS.wallSkill[key];

                //console.log(key,sign,_GAMEDATAHELPER.hasTeamSkill(sign))

                if(!_GAMEDATAHELPER.hasTeamSkill(sign)){

                    if(tiledMap.getLayer(key).getTileGIDAt(titlepos)!=0){
                        return false;
                    }

                }

            }

        }

        return true;

    },


    //根据节点实际坐标判断单元格是否能移动
    isMapPointCanTouch:function(tiledMap,x,y,h,w){

      var mappos = _MANAGERMAP.getMapPos(x,y,h,w);
        //var titlepos = cc.v2(mappos.x,mappos.y);
       return _MANAGERMAP.isMapPointCanTouchByMap(tiledMap,mappos.x,mappos.y,h,w);

    },

    //判断坐标点是否有障碍
    isMapPointHashObj:function(x,y){

        if(
            _MAP
            &&(
                _MAP.npcMap[x+"-"+y]
                ||_MAP.jumpMap[x+"-"+y]
                ||_MAP.boxMap[x+"-"+y]
                ||_MAP.bossMap[x+"-"+y]
                ||_MAP.randomBossMap[x+"-"+y]
                ||_MAP.normalNpcMap[x+"-"+y]
                ||_MAP.chapterMap[x+"-"+y]
            )
        ){
            return true;
        }

        return false;

    },

    

    //根据节点内坐标获取地图对应单元格节点坐标
    getMapNodePosByNode:function(x,y,h,w){

       var nodepos =  _MANAGERMAP.getMapPos(x,y,h,w);

       return _MANAGERMAP.getMapNodePosByTiled(nodepos.x,nodepos.y,h,w);

    },

    //获取地图对象层坐标
    getMapObjPos:function(mapObj){
        return {
            x:Math.floor(mapObj.offset.x/_MANAGERMAP.CELLSIZE),
            y:Math.floor(mapObj.offset.y/_MANAGERMAP.CELLSIZE),
        };
    },


    //根据范围坐标获取准确坐标
    getDirectPosByRealPos:function(x,y,h,w){
        var mapx = Math.floor(x/_MANAGERMAP.CELLSIZE);
        var mapy = Math.floor(y/_MANAGERMAP.CELLSIZE);
        return _MANAGERMAP.getMapNodePosByTiled(mapx,mapy,h,w)
    },

    //根据地图单元格坐标获取节点内坐标
    getMapNodePosByTiled:function(x,y,h,w){

        var sp = _MANAGERMAP.getMapStartPoint(h,w);
        var sx = sp.x;
        var sy = sp.y;


        return cc.v2(sx+_MANAGERMAP.CELLSIZE*(x+0.5),sy-_MANAGERMAP.CELLSIZE*(y+0.5));
    },

    //根据实际坐标获取相对坐标
    getMapPos:function(x,y,h,w){

        var sp = _MANAGERMAP.getMapStartPoint(h,w);
        var sx = sp.x;
        var sy = sp.y;
        return {
            x:Math.floor((x-sx)/_MANAGERMAP.CELLSIZE),
            y:Math.floor((sy-y)/_MANAGERMAP.CELLSIZE),
        };

    },

    //获取地图左上角坐标 h w 皆为奇数
    getMapStartPoint:function(h,w){

        return {
            x:-w/2*_MANAGERMAP.CELLSIZE,
            y:h/2*_MANAGERMAP.CELLSIZE,
        };

    }

}